<?php 
if(KT!='KhiThe') exit;
class sitemap_controller extends sitemap_model{
		
	public function __construct()
	{
		global $lang;
		$lang['sms']='';
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
	}
	
	public function index_action()
	{		 
		$this->lang['li_menunews'] = $this->get_cat_sites('0');

		$resultHtml = $this->khithe_display('index');
		$strHtmlContent = $this->khithe_assign($resultHtml, $this->lang);		
		$strXml ='<?xml version="1.0" encoding="UTF-8"?>';
		$strXml .="\r\n";
		$strXml .='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9             http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
		$strXml .="\r\n\r\n";
		$strXml .= $this->get_cat_site('0');
		$strXml .="\r\n\r\n</urlset>";
		file_put_contents(path_base.'/sitemap.xml',$strXml);
		
		return $strHtmlContent;
	}
	
	public function get_cat_site($ID,$SeoKey=Null)
	{		
		$str = '';
		$query = parent::getMenu(categorys,$ID);
		$total = mysql_num_rows($query);
		if($total){
			
			while($row = mysql_fetch_assoc($query))
			{
                $seokey = ($row['IsPage']==1)?$row["SeoKey"].'.html':$row["SeoKey"].'/';
				$str .="<url>\r\n";
					$str .="\t<loc>".url_base . $seokey ."</loc>\r\n";
					
					$str .="\t<changefreq>always</changefreq>";
					$str .="\t<priority>0.9</priority>\r\n";
				$str .="</url>\r\n\r\n";
				
				$queryp=parent::getAllVal(news,$row["ID"]);
				$totalp = mysql_num_rows($queryp);
				if($totalp){
					while($rowp = mysql_fetch_assoc($queryp))
					{

						$str .="<url>\r\n";
							$str .="\t<loc>".url_base .$row["SeoKey"]."/".$rowp["SeoKey"].".html</loc>\r\n";
							
							$str .="\t<changefreq>always</changefreq>";
							$str .="\t<priority>0.9</priority>\r\n";
						$str .="</url>\r\n\r\n";
					
					}
				
				}
				
				if($row["ID"])
				$str .= $this->get_cat_site($row["ID"],$row["SeoKey"]);
			}
			
		}	
		return $str;
	}
	

	
	public function get_cat_sites($ID,$SeoKey=Null,$i=0)
	{		
		$str = '';
		$query = parent::getMenuNews($ID);
		$total = mysql_num_rows($query);
		if($total){
			$ix = $i;
			$i++;
			$str .= ($ix==0?NULL:'<ul>');
			while($row = mysql_fetch_assoc($query))
			{
                $seokey = ($row['IsPage']==1)?$row["SeoKey"].'.html':$row["SeoKey"].'/';
				$str .='<li><a href="'.url_base . $seokey.'">'.$row["Category"].'</a>';
				$str .= $this->get_cat_sites($row["ID"],$row["SeoKey"],$i);
				$str .= '</li>';				
			}



			$str .= ($ix==0?NULL:'</ul>');
			
			
		}
		return $str;
	}
	


}
?>